package cn.com.cifi.mars.service.impl;

import cn.com.cifi.mars.bean.bo.MdmBuildAreaBO;
import cn.com.cifi.mars.bean.bo.MdmBuildAreaDetailBO;
import cn.com.cifi.mars.bean.bo.SysLogBO;
import cn.com.cifi.mars.bean.dto.GxcLdytDto;
import cn.com.cifi.mars.common.exception.MsgException;
import cn.com.cifi.mars.constant.*;
import cn.com.cifi.mars.entity.MdmBuildArea;
import cn.com.cifi.mars.entity.MdmGroupBld;
import cn.com.cifi.mars.entity.PriceValueOrg;
import cn.com.cifi.mars.mapper.mars.BuildAreaMapper;
import cn.com.cifi.mars.mapper.mars.GroupBldMapper;
import cn.com.cifi.mars.service.BuildAreaService;
import cn.com.cifi.mars.service.SysLogService;
import cn.com.cifi.mars.util.StrUtils;
import cn.com.cifi.mars.util.UUIDUtils;
import cn.com.cifi.mars.util.WebUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
 * <p>
 * （主数据）楼栋业态面积表 服务实现类
 * </p>
 *
 * @author yyd
 * @since 2019-09-24
 */
@Service
public class BuildAreaServiceImpl extends ServiceImpl<BuildAreaMapper, MdmBuildArea> implements BuildAreaService {

    @Autowired
    private BuildAreaMapper buildAreaMapper;

    @Autowired
    private GroupBldMapper groupBldMapper;

    @Autowired
    private SysLogService sysLogService;

    @Override
    @Transactional
    public void addDataBuildArea(List<GxcLdytDto> list, LocalDateTime now,String timestamp_id) {
        List<MdmBuildArea> buildAreaList = new ArrayList<>();
        if (list.size() > 0){
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            for (GxcLdytDto gxcLdytDto : list) {
                MdmBuildArea buildArea = new MdmBuildArea();
                buildArea.setDesignBuildId(gxcLdytDto.getDesignBuildId());
                buildArea.setBldPrdId(gxcLdytDto.getBldPrdId());
                buildArea.setFormatBuildingCode(gxcLdytDto.getFormatBuildingCode());
                buildArea.setFormatBuildingName(gxcLdytDto.getFormatBuildingName());
                buildArea.setBuildId(gxcLdytDto.getBuildId());
                buildArea.setBuildingCode(gxcLdytDto.getBuildingCode());
                buildArea.setBuildingName(gxcLdytDto.getBuildingName());
                buildArea.setProjectfId(gxcLdytDto.getProjectfId());
                buildArea.setKingdeeProjectFid(gxcLdytDto.getKingdeeProjectfId());
                buildArea.setStageCode(gxcLdytDto.getStageCode());
                buildArea.setVersionStage(gxcLdytDto.getVersionStage());
                buildArea.setVersionNum(gxcLdytDto.getVersionNum());
                buildArea.setProductCode(gxcLdytDto.getProductCode());
                buildArea.setProductType(gxcLdytDto.getProductType());
                buildArea.setBusinessType(gxcLdytDto.getBusinessType());
                buildArea.setBusinessTypeName(gxcLdytDto.getBusinessTypeName());
                buildArea.setFreeType(gxcLdytDto.getFreeType());
                buildArea.setFreeTypeName(gxcLdytDto.getFreeTypeName());
                buildArea.setProType(gxcLdytDto.getProType());
                buildArea.setProTypeName(gxcLdytDto.getProTypeName());
                if (null != gxcLdytDto.getBaseBuildArea()){
                    buildArea.setBaseBuildArea(new BigDecimal(gxcLdytDto.getBaseBuildArea()));
                }
                if (null != gxcLdytDto.getTotalSaleArea()){
                    buildArea.setTotalSaleArea(new BigDecimal(gxcLdytDto.getTotalSaleArea()));
                }
                if (null != gxcLdytDto.getTotalBuildArea()){
                    buildArea.setTotalBuildArea(new BigDecimal(gxcLdytDto.getTotalBuildArea()));
                }
                if (null != gxcLdytDto.getUpBuildArea()){
                    buildArea.setUpBuildArea(new BigDecimal(gxcLdytDto.getUpBuildArea()));
                }
                if (null != gxcLdytDto.getUnderBuildArea()){
                    buildArea.setUnderBuildArea(new BigDecimal(gxcLdytDto.getUnderBuildArea()));
                }
                if (null != gxcLdytDto.getUpSaleArea()){
                    buildArea.setUpSaleArea(new BigDecimal(gxcLdytDto.getUpSaleArea()));
                }
                if (null != gxcLdytDto.getUnderSaleArea()){
                    buildArea.setUnderSaleArea(new BigDecimal(gxcLdytDto.getUnderSaleArea()));
                }
                if (null != gxcLdytDto.getTotalcomplimentaryarea()){
                    buildArea.setTotalcomplimentaryarea(new BigDecimal(gxcLdytDto.getTotalcomplimentaryarea()));
                }
                if (null != gxcLdytDto.getUpGiftArea()){
                    buildArea.setUpGiftArea(new BigDecimal(gxcLdytDto.getUpGiftArea()));
                }
                if (null != gxcLdytDto.getUnderGiftArea()){
                    buildArea.setUnderGiftArea(new BigDecimal(gxcLdytDto.getUnderGiftArea()));
                }
                if (null != gxcLdytDto.getTotalLandArea()){
                    buildArea.setTotalLandArea(new BigDecimal(gxcLdytDto.getTotalLandArea()));
                }
                if (null != gxcLdytDto.getFloors()){
                    buildArea.setFloors(Integer.parseInt(gxcLdytDto.getFloors().trim()));
                }
                if (null != gxcLdytDto.getUnitNum()){
                    buildArea.setUnitNum(Integer.parseInt(gxcLdytDto.getUnitNum().trim()));
                }
                if (null != gxcLdytDto.getRoomHigh()){
                    buildArea.setRoomHigh(new BigDecimal(gxcLdytDto.getRoomHigh()));
                }
                if (null != gxcLdytDto.getFloorHigh()){
                    buildArea.setFloorHigh(new BigDecimal(gxcLdytDto.getFloorHigh()));
                }
                if (null != gxcLdytDto.getLiftNum()){
                    buildArea.setLiftNum(Integer.parseInt(gxcLdytDto.getLiftNum().trim()));
                }
                if (null != gxcLdytDto.getRoomNum()){
                    buildArea.setRoomNum(Integer.parseInt(gxcLdytDto.getRoomNum().trim()));
                }
                if (null != gxcLdytDto.getStructureArea()){
                    buildArea.setStructureArea(new BigDecimal(gxcLdytDto.getStructureArea()));
                }
                if (null != gxcLdytDto.getParkingRatio()){
                    buildArea.setParkingRatio(new BigDecimal(gxcLdytDto.getParkingRatio()));
                }
                if (null != gxcLdytDto.getEndDate()){
                    buildArea.setEndDate(LocalDateTime.parse(gxcLdytDto.getEndDate(),formatter));
                }
                if (null != gxcLdytDto.getCreatedTime()){
                    buildArea.setCreatedTime(LocalDateTime.parse(gxcLdytDto.getCreatedTime(),formatter));
                }
                if (null != gxcLdytDto.getUpdatedTime()){
                    buildArea.setUpdatedTime(LocalDateTime.parse(gxcLdytDto.getUpdatedTime(),formatter));
                }
                buildArea.setCreatedBy(gxcLdytDto.getCreatedBy());
                buildArea.setUpdatedBy(gxcLdytDto.getUpdatedBy());
                buildArea.setIsDelete(gxcLdytDto.getIsDelete());
                buildArea.setBatchCode(gxcLdytDto.getBatchCode());
                buildArea.setDeliveryStandard(gxcLdytDto.getDeliveryStandard());
                buildArea.setPoliceCode(gxcLdytDto.getPoliceCode());
                buildArea.setCalcMethod(gxcLdytDto.getCalcMethod());

                buildArea.setSysCreatedTime(now);
                buildArea.setTimestampId(timestamp_id);
                
                buildAreaList.add(buildArea);
            }
        }
        buildAreaMapper.insertMdmDataList(buildAreaList);
    }

    @Override
    @Transactional
    public Map<String, String> saveArea(String buildFormatAreaInfo, Map<String, String> stageMapIds, LocalDateTime now) {

        List<MdmBuildArea> dataList = Collections.synchronizedList(new ArrayList<>());
        JSONObject buildFormatAreaInfoObj =  JSON.parseObject(buildFormatAreaInfo);
        JSONArray buildFormatAreaArray = buildFormatAreaInfoObj.getJSONObject("requestInfo").getJSONObject("buildFormatArea").getJSONArray("buildFormatAreaInfoLine");
        if(buildFormatAreaArray != null && !buildFormatAreaArray.isEmpty()) {
            String timestamp_id = UUIDUtils.create();
            for(int i =0; i < buildFormatAreaArray.size(); i ++ ) {
                JSONObject  buildFormatAreaObj = buildFormatAreaArray.getJSONObject(i);
                MdmBuildArea buildArea = createBuildArea(stageMapIds, now, timestamp_id, buildFormatAreaObj);
                dataList.add(buildArea);
            }
        }

        //saveBatch(dataList);
        buildAreaMapper.insertBatch(dataList);

        return stageMapIds;
    }

	private MdmBuildArea createBuildArea(Map<String, String> stageMapIds, LocalDateTime now, String timestamp_id,
			JSONObject buildFormatAreaObj) {
		MdmBuildArea buildArea = new MdmBuildArea();
		buildArea.setDesignBuildId(buildFormatAreaObj.getString("designBuildID"));
		buildArea.setBldPrdId(buildFormatAreaObj.getString("bldPrdID"));
		buildArea.setFormatBuildingCode(buildFormatAreaObj.getString("formatBuildingCode"));
		buildArea.setFormatBuildingName(buildFormatAreaObj.getString("formatBuildingName"));
		buildArea.setBuildId(buildFormatAreaObj.getString("buildID"));
		buildArea.setBuildingCode(buildFormatAreaObj.getString("buildingCode"));
		buildArea.setBuildingName(buildFormatAreaObj.getString("buildingName"));
		buildArea.setProjectfId(buildFormatAreaObj.getString("projectFID"));
		buildArea.setKingdeeProjectFid(buildFormatAreaObj.getString("kingdeeProjectFID"));
		buildArea.setStageCode(buildFormatAreaObj.getString("stageCode"));
		buildArea.setVersionStage(buildFormatAreaObj.getString("versionStage"));
		stageMapIds.put(buildFormatAreaObj.getString("projectFID"), buildFormatAreaObj.getString("versionStage"));

		buildArea.setVersionNum(buildFormatAreaObj.getString("versionNum"));//接收版本号
		buildArea.setProductCode(buildFormatAreaObj.getString("productCode"));
		buildArea.setProductType(buildFormatAreaObj.getString("productType"));
		buildArea.setBusinessType(buildFormatAreaObj.getString("businessType"));
		buildArea.setBusinessTypeName(buildFormatAreaObj.getString("businessTypeName"));
		buildArea.setFreeType(buildFormatAreaObj.getString("freeType"));
		buildArea.setFreeTypeName(buildFormatAreaObj.getString("freeTypeName"));
		buildArea.setProType(buildFormatAreaObj.getString("proType"));
		buildArea.setProTypeName(buildFormatAreaObj.getString("proTypeName"));
		buildArea.setBaseBuildArea(StrUtils.stringToBigDecimal(buildFormatAreaObj.getString("baseBuildArea")));
		buildArea.setTotalSaleArea(StrUtils.stringToBigDecimal(buildFormatAreaObj.getString("totalSaleArea")));
		buildArea.setTotalBuildArea(StrUtils.stringToBigDecimal(buildFormatAreaObj.getString("totalBuildArea")));
		buildArea.setUpBuildArea(StrUtils.stringToBigDecimal(buildFormatAreaObj.getString("upBuildArea")));
		buildArea.setUnderBuildArea(StrUtils.stringToBigDecimal(buildFormatAreaObj.getString("underBuildArea")));
		buildArea.setUpSaleArea(StrUtils.stringToBigDecimal(buildFormatAreaObj.getString("upSaleArea")));
		buildArea.setUnderSaleArea(StrUtils.stringToBigDecimal(buildFormatAreaObj.getString("underSaleArea")));
		buildArea.setTotalcomplimentaryarea(StrUtils.stringToBigDecimal(buildFormatAreaObj.getString("totalComplimentaryArea")));
		buildArea.setUpGiftArea(StrUtils.stringToBigDecimal(buildFormatAreaObj.getString("upGiftArea")));
		buildArea.setUnderGiftArea(StrUtils.stringToBigDecimal(buildFormatAreaObj.getString("underGiftArea")));
		buildArea.setTotalLandArea(StrUtils.stringToBigDecimal(buildFormatAreaObj.getString("totalLandArea")));
		buildArea.setFloors(StrUtils.stringToInteger(buildFormatAreaObj.getString("floors")));
		buildArea.setUnitNum(StrUtils.stringToInteger(buildFormatAreaObj.getString("unitNum")));
		buildArea.setRoomHigh(StrUtils.stringToBigDecimal(buildFormatAreaObj.getString("roomHigh")));
		buildArea.setFloorHigh(StrUtils.stringToBigDecimal(buildFormatAreaObj.getString("floorHigh")));
		buildArea.setLiftNum(StrUtils.stringToInteger(buildFormatAreaObj.getString("liftNum")));
		buildArea.setRoomNum(StrUtils.stringToInteger(buildFormatAreaObj.getString("roomNum")));
		buildArea.setStructureArea(StrUtils.stringToBigDecimal(buildFormatAreaObj.getString("structureArea")));
		buildArea.setParkingRatio(StrUtils.stringToBigDecimal(buildFormatAreaObj.getString("parkingRatio")));
		if(buildFormatAreaObj.getDate("endDate") != null){
		    buildArea.setEndDate(LocalDateTime.ofInstant(buildFormatAreaObj.getDate("endDate").toInstant(), ZoneId.systemDefault()));
		}
		buildArea.setBatchCode(buildFormatAreaObj.getString("batchCode"));
		buildArea.setDeliveryStandard(buildFormatAreaObj.getString("deliveryStandard"));
		buildArea.setPoliceCode(buildFormatAreaObj.getString("policeCode"));
		buildArea.setCalcMethod(buildFormatAreaObj.getString("calcMethod"));
		Date createdTime = buildFormatAreaObj.getDate("createdTime");
		if(createdTime != null){
		    buildArea.setCreatedTime(LocalDateTime.ofInstant(buildFormatAreaObj.getDate("createdTime").toInstant(), ZoneId.systemDefault()));
		}
		buildArea.setCreatedBy(buildFormatAreaObj.getString("createdBy"));
		Date updatedTime = buildFormatAreaObj.getDate("updatedTime");
		if(updatedTime != null){
		    buildArea.setCreatedTime(LocalDateTime.ofInstant(buildFormatAreaObj.getDate("updatedTime").toInstant(), ZoneId.systemDefault()));
		}
		buildArea.setUpdatedBy(buildFormatAreaObj.getString("updatedBy"));
		if(StringUtils.isNotEmpty(buildFormatAreaObj.getString("isDelete"))) {
		    buildArea.setIsDelete(buildFormatAreaObj.getString("isDelete"));
		}else{
		    buildArea.setIsDelete(IsDeleteEnum.NO.getKey()+"");
		}

		buildArea.setSysCreatedTime(now);
		buildArea.setTimestampId(timestamp_id);
		return buildArea;
	}

    @Override
    @Transactional
    public void inserBatch(List<MdmBuildArea> gxcLdytDtolIST) {
        buildAreaMapper.insertBatch(gxcLdytDtolIST);
    }

    @Override
    public MdmBuildAreaBO selectNewestByFid(String projectFid, String versionType, int isDel) {
        MdmBuildAreaBO buildAreaBO = new MdmBuildAreaBO();

        List<String> versionTypes = new ArrayList<>();
        versionTypes.add(VersionTypeEunm.WARPLAN.getValue());
        if(VersionTypeEunm.GETPRICE.getKey().equals(versionType)){
            versionTypes.add(VersionTypeEunm.PERMIT.getValue());
            versionTypes.add(VersionTypeEunm.PRESURVEY.getValue());
            versionTypes.add(VersionTypeEunm.ACTUALSURVEY.getValue());
        }else if(VersionTypeEunm.DYNAMIC.getKey().equals(versionType)){
            versionTypes.add(VersionTypeEunm.PERMIT.getValue());
            versionTypes.add(VersionTypeEunm.PRESURVEY.getValue());
            versionTypes.add(VersionTypeEunm.ACTUALSURVEY.getValue());
        }

        //查询最新版本类型的面积
        MdmBuildArea buildArea = buildAreaMapper.selectNewestByFid(projectFid, versionTypes, isDel);
        //从（主数据）组团楼栋关系表和（主数据）楼栋业态面积表(关联查询),取分期对应相关数据
        if(buildArea != null){
            String versionStage = buildArea.getVersionStage();
            String versionNum = buildArea.getVersionNum();
            LocalDateTime areaCreateTime = buildArea.getSysCreatedTime();
            //引用最新审批通过版本的主数据
            MdmGroupBld groupBlds = groupBldMapper.selectNewestCreateTimeByFid(projectFid, versionStage, versionNum, isDel);
            if(groupBlds == null){
                throw new MsgException(CodeEnum.NON_EXISTENT_SUPPLY_AREAATYPISM_VERSIONS);
            }
            LocalDateTime groupCreateTime = groupBlds.getCreateTime();
            String versionCode = groupBlds.getVersionCode();

            //引用数据详情
            buildAreaBO.setSourceAreaStage(buildArea.getVersionStage());
            buildAreaBO.setSourceAreaNum(buildArea.getVersionNum());
            buildAreaBO.setSourceAreaCreatedTime(buildArea.getCreatedTime());
            buildAreaBO.setSourceAreaSysCreatedTime(buildArea.getSysCreatedTime());
            buildAreaBO.setSourceGroupStage(groupBlds.getVersionStage());
            buildAreaBO.setSourceGroupNum(groupBlds.getVersionCode());
            buildAreaBO.setSourceGroupApproval(groupBlds.getApprovalTime());
            buildAreaBO.setSourceGroupSysCreatedTime(groupBlds.getCreateTime());
            buildAreaBO.setTimestampId(buildArea.getTimestampId());

            //分期
            MdmBuildAreaDetailBO projectfItem = buildAreaMapper.selectNewestProjectFByFid(projectFid, versionStage, areaCreateTime, groupCreateTime, versionNum, versionCode, isDel);
            if(projectfItem == null){
                CodeEnum.NON_PANOPLAN_PROJECTF.setMessage(CodeEnum.NON_PANOPLAN_PROJECTF.getMessage());
                CodeEnum.NON_PANOPLAN_PROJECTF.setMessage("主数据 "+CodeEnum.NON_PANOPLAN_PROJECTF.getMessage());
                throw new MsgException(CodeEnum.NON_PANOPLAN_PROJECTF);
            }
            buildAreaBO.setProjectFItems(projectfItem);

            //组团
            List<MdmBuildAreaDetailBO> groupItems = buildAreaMapper.selectNewestGroupByFid(projectFid, versionStage, areaCreateTime, groupCreateTime, versionNum, versionCode, isDel);
            if(CollectionUtils.isEmpty(groupItems)){
                CodeEnum.NON_PANOPLAN_GROUP.setMessage(CodeEnum.NON_PANOPLAN_GROUP.getMessage());
                CodeEnum.NON_PANOPLAN_GROUP.setMessage("主数据 "+CodeEnum.NON_PANOPLAN_GROUP.getMessage());
                throw new MsgException(CodeEnum.NON_PANOPLAN_GROUP);
            }
            buildAreaBO.setGroupItems(groupItems);

            //产品构成
            List<MdmBuildAreaDetailBO> productItems = buildAreaMapper.selectNewestProductByFid(projectFid, versionStage, areaCreateTime, groupCreateTime, versionNum, versionCode, isDel);
            if(CollectionUtils.isEmpty(productItems)){
                CodeEnum.NON_PANOPLAN_PRODUT.setMessage(CodeEnum.NON_PANOPLAN_PRODUT.getMessage());
                CodeEnum.NON_PANOPLAN_PRODUT.setMessage("主数据 "+CodeEnum.NON_PANOPLAN_PRODUT.getMessage());
                throw new MsgException(CodeEnum.NON_PANOPLAN_PRODUT);
            }
            buildAreaBO.setProductItems(productItems);

            //楼栋
            List<MdmBuildAreaDetailBO> buildItems = buildAreaMapper.selectNewestBuildByFid(projectFid, versionStage, areaCreateTime, groupCreateTime, versionNum, versionCode, isDel);
            if(CollectionUtils.isEmpty(buildItems)){
                CodeEnum.NON_PANOPLAN_BUILD.setMessage(CodeEnum.NON_PANOPLAN_BUILD.getMessage());
                CodeEnum.NON_PANOPLAN_BUILD.setMessage("主数据 "+CodeEnum.NON_PANOPLAN_BUILD.getMessage());
                throw new MsgException(CodeEnum.NON_PANOPLAN_BUILD);
            }
            buildAreaBO.setBuildItems(buildItems);

            return buildAreaBO;
        }else{
            throw new MsgException(CodeEnum.NON_GROUPBLD_AREA);
        }
    }

    @Override
    public MdmBuildAreaBO selectNewestByFidAndVersionType(String projectFid, String versionType, int isDel) {
        MdmBuildAreaBO buildAreaBO = new MdmBuildAreaBO();

        //查询最新版本类型的面积
        MdmBuildArea buildArea = buildAreaMapper.selectNewestByFid(projectFid, Arrays.asList(versionType), isDel);
        //从（主数据）组团楼栋关系表和（主数据）楼栋业态面积表(关联查询),取分期对应相关数据
        if(buildArea != null){
            String versionStage = buildArea.getVersionStage();
            String versionNum = buildArea.getVersionNum();
            LocalDateTime areaCreateTime = buildArea.getSysCreatedTime();
            //引用最新审批通过版本的主数据
            MdmGroupBld groupBlds = groupBldMapper.selectNewestCreateTimeByFid(projectFid, versionStage, versionNum, isDel);
            if(groupBlds == null){
                throw new MsgException(CodeEnum.NON_EXISTENT_SUPPLY_AREAATYPISM_VERSIONS);
            }
            LocalDateTime groupCreateTime = groupBlds.getCreateTime();
            String versionCode = groupBlds.getVersionCode();

            //引用数据详情
            buildAreaBO.setSourceAreaStage(buildArea.getVersionStage());
            buildAreaBO.setSourceAreaNum(buildArea.getVersionNum());
            buildAreaBO.setSourceAreaCreatedTime(buildArea.getCreatedTime());
            buildAreaBO.setSourceAreaSysCreatedTime(buildArea.getSysCreatedTime());
            buildAreaBO.setSourceGroupStage(groupBlds.getVersionStage());
            buildAreaBO.setSourceGroupNum(groupBlds.getVersionCode());
            buildAreaBO.setSourceGroupApproval(groupBlds.getApprovalTime());
            buildAreaBO.setSourceGroupSysCreatedTime(groupBlds.getCreateTime());
            buildAreaBO.setTimestampId(buildArea.getTimestampId());

            //分期
            MdmBuildAreaDetailBO projectfItem = buildAreaMapper.selectNewestProjectFByFid(projectFid, versionStage, areaCreateTime, groupCreateTime, versionNum, versionCode, isDel);
            if(projectfItem == null){
                CodeEnum.NON_PANOPLAN_PROJECTF.setMessage("主数据 "+CodeEnum.NON_PANOPLAN_PROJECTF.getMessage());
                throw new MsgException(CodeEnum.NON_PANOPLAN_PROJECTF);
            }
            buildAreaBO.setProjectFItems(projectfItem);

            //组团
            List<MdmBuildAreaDetailBO> groupItems = buildAreaMapper.selectNewestGroupByFid(projectFid, versionStage, areaCreateTime, groupCreateTime, versionNum, versionCode, isDel);
            if(CollectionUtils.isEmpty(groupItems)){
                CodeEnum.NON_PANOPLAN_GROUP.setMessage("主数据 "+CodeEnum.NON_PANOPLAN_GROUP.getMessage());
                throw new MsgException(CodeEnum.NON_PANOPLAN_GROUP);
            }
            buildAreaBO.setGroupItems(groupItems);

            //产品构成
            List<MdmBuildAreaDetailBO> productItems = buildAreaMapper.selectNewestProductByFid(projectFid, versionStage, areaCreateTime, groupCreateTime, versionNum, versionCode, isDel);
            if(CollectionUtils.isEmpty(productItems)){
                CodeEnum.NON_PANOPLAN_PRODUT.setMessage("主数据 "+CodeEnum.NON_PANOPLAN_PRODUT.getMessage());
                throw new MsgException(CodeEnum.NON_PANOPLAN_PRODUT);
            }
            buildAreaBO.setProductItems(productItems);

            //楼栋
            List<MdmBuildAreaDetailBO> buildItems = buildAreaMapper.selectNewestBuildByFid(projectFid, versionStage, areaCreateTime, groupCreateTime, versionNum, versionCode, isDel);
            if(CollectionUtils.isEmpty(buildItems)){
                CodeEnum.NON_PANOPLAN_BUILD.setMessage("主数据 "+CodeEnum.NON_PANOPLAN_BUILD.getMessage());
                throw new MsgException(CodeEnum.NON_PANOPLAN_BUILD);
            }
            buildAreaBO.setBuildItems(buildItems);

            return buildAreaBO;
        }

        return null;
    }

    @Override
    public MdmBuildAreaBO selectAppoint(String projectfId, String sourceAreaVersionStage, String sourceAreaVersionNum, LocalDateTime sourceAreaSysCreatedTime, String sourceGroupVersionStage, String sourceGroupVersionCode, LocalDateTime sourceGroupCreateTime) {
        MdmBuildAreaBO buildAreaBO = new MdmBuildAreaBO();
        //查询楼栋业态面积版本
        QueryWrapper<MdmBuildArea> areaWrapper = new QueryWrapper<MdmBuildArea>();
        areaWrapper.eq("version_stage", sourceAreaVersionStage)
                .eq("version_num", sourceAreaVersionNum)
                .eq("sys_created_time", sourceAreaSysCreatedTime)
                .eq("projectf_id", projectfId);
        List<MdmBuildArea> buildAreaList = buildAreaMapper.selectList(areaWrapper);
        MdmBuildArea buildArea = null;
        if (CollectionUtils.isNotEmpty(buildAreaList)){
            buildArea = buildAreaList.get(0);
        }
        if (buildArea == null){
            throw new MsgException(CodeEnum.NON_EXISTENT_SUPPLY_AREAATYPISM_VERSIONS);
        }
        //查询组团楼栋关系版本
        QueryWrapper<MdmGroupBld> groupWrapper = new QueryWrapper<MdmGroupBld>();
        groupWrapper.eq("version_stage", sourceGroupVersionStage)
                .eq("version_code", sourceGroupVersionCode)
                .eq("create_time", sourceGroupCreateTime)
                .eq("projectf_id", projectfId);
        List<MdmGroupBld> mdmGroupBlds = groupBldMapper.selectList(groupWrapper);
        MdmGroupBld groupBlds = null;
        if (CollectionUtils.isNotEmpty(mdmGroupBlds)){
            groupBlds = mdmGroupBlds.get(0);
        }
        if(groupBlds == null){
            throw new MsgException(CodeEnum.NON_EXISTENT_SUPPLY_AREAATYPISM_VERSIONS);
        }

        //引用数据详情
        buildAreaBO.setSourceAreaStage(buildArea.getVersionStage());
        buildAreaBO.setSourceAreaNum(buildArea.getVersionNum());
        buildAreaBO.setSourceAreaCreatedTime(buildArea.getCreatedTime());
        buildAreaBO.setSourceAreaSysCreatedTime(buildArea.getSysCreatedTime());
        buildAreaBO.setSourceGroupStage(groupBlds.getVersionStage());
        buildAreaBO.setSourceGroupNum(groupBlds.getVersionCode());
        buildAreaBO.setSourceGroupApproval(groupBlds.getApprovalTime());
        buildAreaBO.setSourceGroupSysCreatedTime(groupBlds.getCreateTime());
        buildAreaBO.setTimestampId(buildArea.getTimestampId());

        //分期
        MdmBuildAreaDetailBO projectfItem = buildAreaMapper.selectNewestProjectFByFid(projectfId, sourceAreaVersionStage, sourceAreaSysCreatedTime, sourceGroupCreateTime, sourceAreaVersionNum, sourceGroupVersionCode, IsDeleteEnum.NO.getKey());
        if(projectfItem == null){
            CodeEnum.NON_PANOPLAN_PROJECTF.setMessage("主数据 "+CodeEnum.NON_PANOPLAN_PROJECTF.getMessage());
            throw new MsgException(CodeEnum.NON_PANOPLAN_PROJECTF);
        }
        buildAreaBO.setProjectFItems(projectfItem);

        //组团
        List<MdmBuildAreaDetailBO> groupItems = buildAreaMapper.selectNewestGroupByFid(projectfId, sourceAreaVersionStage, sourceAreaSysCreatedTime, sourceGroupCreateTime, sourceAreaVersionNum, sourceGroupVersionCode, IsDeleteEnum.NO.getKey());
        if(CollectionUtils.isEmpty(groupItems)){
            CodeEnum.NON_PANOPLAN_GROUP.setMessage("主数据 "+CodeEnum.NON_PANOPLAN_GROUP.getMessage());
            throw new MsgException(CodeEnum.NON_PANOPLAN_GROUP);
        }
        buildAreaBO.setGroupItems(groupItems);

        //产品构成
        List<MdmBuildAreaDetailBO> productItems = buildAreaMapper.selectNewestProductByFid(projectfId, sourceAreaVersionStage, sourceAreaSysCreatedTime, sourceGroupCreateTime, sourceAreaVersionNum, sourceGroupVersionCode, IsDeleteEnum.NO.getKey());
        if(CollectionUtils.isEmpty(productItems)){
            CodeEnum.NON_PANOPLAN_PRODUT.setMessage("主数据 "+CodeEnum.NON_PANOPLAN_PRODUT.getMessage());
            throw new MsgException(CodeEnum.NON_PANOPLAN_PRODUT);
        }
        buildAreaBO.setProductItems(productItems);

        //楼栋
        List<MdmBuildAreaDetailBO> buildItems = buildAreaMapper.selectNewestBuildByFid(projectfId, sourceAreaVersionStage, sourceAreaSysCreatedTime, sourceGroupCreateTime, sourceAreaVersionNum, sourceGroupVersionCode, IsDeleteEnum.NO.getKey());
        if(CollectionUtils.isEmpty(buildItems)){
            CodeEnum.NON_PANOPLAN_BUILD.setMessage("主数据 "+CodeEnum.NON_PANOPLAN_BUILD.getMessage());
            throw new MsgException(CodeEnum.NON_PANOPLAN_BUILD);
        }
        buildAreaBO.setBuildItems(buildItems);

        return buildAreaBO;
    }
}
